package com.suiwei.base.controller;

import com.suiwei.mybatisPlus.entity.Book;
import com.suiwei.base.global.GlobalResponse;
import com.suiwei.mybatisPlus.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author suiwei
 * @date 2022-10-22 22:28
 * restful风格
 */
@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping
    public GlobalResponse getAll() {
        log.info(new Date().toString());
        return new GlobalResponse(true, bookService.list());
    }

    /**
     * 添加
     */
    @PostMapping
    public Boolean save(@RequestBody Book book) {
        return bookService.save(book);
    }

    /**
     * 修改
     *
     * @param book
     * @return
     */
    @PutMapping
    public Boolean update(@RequestBody Book book) {
        return bookService.updateById(book);
    }

    /**
     * 删除
     */
    @DeleteMapping("{id}")
    public GlobalResponse delete(@PathVariable Integer id) {
        return new GlobalResponse(bookService.removeById(id));
    }

}
